package com.zj.docker.component;

import com.zj.docker.client.LogClient;
import com.zj.docker.enums.LogEnum;
import com.zj.docker.utils.RandomUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Auther: zj
 * @Date: 2019/2/4 17:40
 * @Description:
 */
@Component
//@Service
public class LogComponent {

    @Autowired
    LogClient logClient;
    @Autowired
    UserRoleAuthentication userRoleAuthentication;
    /**
     * 写入日志（前提正常）
     * @param request
     * @param logEnum
     */
    public void saveLog(HttpServletRequest request, LogEnum logEnum) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳

        logClient.createLog( RandomUtils.uuid(),userRoleAuthentication.getUsernameFromRequest( request ),
                "",logEnum.getCode(),logEnum.getMessage(),request.getRequestURI(),request.getMethod(),
                getRemoteAddr( request),request.getHeader("user-agent"),"","",date);

    }

    /**
     * 写入日志（前提出现异常）
     * @param request
     * @param logEnum
     * @param e
     */
    public void saveLog(HttpServletRequest request, LogEnum logEnum, Exception e) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳

        logClient.createLog( RandomUtils.uuid(),userRoleAuthentication.getUsernameFromRequest( request ),
                "",logEnum.getCode(),logEnum.getMessage(),request.getRequestURI(),request.getMethod(),
                getRemoteAddr( request),request.getHeader("user-agent"),"",e.getMessage(),date);
    }

    /**
     * 写入日志（异步时调用，这真是一个stupid的idea!!!） (前提正常)
     * @param username
     * @param url
     * @param method
     * @param ip
     * @param userAgent
     * @param logEnum
     */
    public void saveLogAsync( String username,String url,String method,String ip,String userAgent,LogEnum logEnum){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳


        logClient.createLog( RandomUtils.uuid(),username,
                    "",logEnum.getCode(),logEnum.getMessage(),url,method,
                    ip,userAgent,"",null,date);


    }

    /**
     * 写入日志（异步时调用，这真是一个stupid的idea!!!） (前提异常)
     * @param username
     * @param url
     * @param method
     * @param ip
     * @param userAgent
     * @param logEnum
     * @param e
     */
    public void saveLogAsync( String username,String url,String method,String ip,String userAgent,LogEnum logEnum, Exception e){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳

        logClient.createLog( RandomUtils.uuid(),username,
                "",logEnum.getCode(),logEnum.getMessage(),url,method,
                ip,userAgent,"",e.getMessage(),date);
    }

    public static String getRemoteAddr(HttpServletRequest request) {
        String remoteAddr = request.getHeader("X-Real-IP");
        if (!StringUtils.isBlank(remoteAddr)) {
            remoteAddr = request.getHeader("X-Forwarded-For");
        } else if (!StringUtils.isBlank(remoteAddr)) {
            remoteAddr = request.getHeader("Proxy-Client-IP");
        } else if (!StringUtils.isBlank(remoteAddr)) {
            remoteAddr = request.getHeader("WL-Proxy-Client-IP");
        }
        return remoteAddr != null ? remoteAddr : request.getRemoteAddr();
    }

}

